<?php
namespace app\api\controller;

use app\admin\model\GameStage;
use app\admin\model\GameListing;
use app\admin\model\GameWords;
use app\common\controller\Frontend;
use ba\Tree;

class Mpindex extends Frontend{
    
    protected array $noNeedLogin = ['grade','gameunit','game','config'];
    
    public function initialize(): void
    {
        parent::initialize();
    }
    public function config(){
        $data['gameErrorVedio'] = full_url(get_sys_config('game_error_mp3'));
        $data['gamePengVedio'] = full_url(get_sys_config('game_once_sucess'));
        $data['gameSuccessVedio'] = full_url(get_sys_config('game_all_sucess'));
        $this->success('',$data);
    }
    public function grade(){
        if($this->request->isPost()){
            $data = GameStage::where('status',1)->order(['pid','weigh'=>'DESC'])->select()->toArray();
            $list = (new Tree())->assembleChild($data);
            $this->success('',$list);
        }
    }
    
    public function gameunit(){
        if($this->request->isPost()){
            $id = $this->request->post('id',0);
            $gameunit = GameListing::where('stage_id',$id)->where('status',1)->order('weigh','desc')->select()->toArray();
            if(empty($gameunit)){
                $this->error('');
            }
            $words = GameWords::where('listing_id',$gameunit[0]['id'])->select()->toArray();
            if(empty($words)){
                $this->error('');
            }
            $en = [];
            $zh = [];
            $game = [];
            foreach($words as $key => $value){
                $en[$key]=[
                    'ids'=>$value['id'],
                    'value'=>trim($value['en']),
                    'audio'=>full_url().'/'.$value['speak_en']
                    ];
                $zh[$key]=[
                    'ids'=>$value['id'],
                    'value'=>trim($value['cn']).trim($value['explain']),
                    'audio'=>full_url().'/'.$value['speak_cn']
                    ];
            }
            shuffle($en);
            shuffle($zh);
            $game = array_merge($en,$zh);
             shuffle($game);
             shuffle($game);
            $this->success('',['unit'=>$gameunit,'game'=>$game]);
        }
    }
    
    public function game(){
        if($this->request->isPost()){
            $id = $this->request->post('id',0);
            
            $words = GameWords::where('listing_id',$id)->select()->toArray();
            if(empty($words)){
                $this->error('');
            }
            $en = [];
            $zh = [];
            $game = [];
            foreach($words as $key => $value){
                $en[$key]=[
                    'ids'=>$value['id'],
                    'value'=>trim($value['en']),
                    'audio'=>full_url().'/'.$value['speak_en']
                    ];
                $zh[$key]=[
                    'ids'=>$value['id'],
                    'value'=>trim($value['cn']).trim($value['explain']),
                    'audio'=>full_url().'/'.$value['speak_cn']
                    ];
            }
            shuffle($en);
            shuffle($zh);
            $game = array_merge($en,$zh);
             shuffle($game);
             shuffle($game);
            $this->success('',$game);
        }
    }
}